package com.kadron.creator;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kadron.common.KUtil;
import com.kadron.dao.KDiscountItemDAO;
import com.kadron.dao.KImageDAO;
import com.kadron.dao.KItemDAO;
import com.kadron.datamodel.KModelCreator;
import com.kadron.entity.Discount;
import com.kadron.entity.Item;
import com.kadron.entity.KItemImage;
import com.kadron.item.KItemModel;

public class KItemModelCreator implements KModelCreator {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5743949359869565982L;
	
	private static final Log log = LogFactory.getLog(KItemModelCreator.class);

	private KItemDAO itemDAO;
	private KDiscountItemDAO discountItemDAO;
	private KImageDAO imageDAO;
	
	public KItemModelCreator(KItemDAO itemDAO,
			KDiscountItemDAO discountItemDAO, KImageDAO imageDAO) {
		super();
		this.itemDAO = itemDAO;
		this.discountItemDAO = discountItemDAO;
		this.imageDAO = imageDAO;
	}

	@Override
	public KItemModel create(long id) {
		if (id <= 0) {
			log.error("id is <= 0");
			return null;
		}
		Item item = (Item) itemDAO.find(id, Item.class);
		KItemImage img = imageDAO.getMainItemImage(item.getId());
		Date moment = KUtil.getMoment();
		if (img == null) {
			img = new KItemImage(item.getId());
		}
		KItemModel model = new KItemModel(item,img);
		Discount discount = discountItemDAO.getDiscount(item.getId(), moment);
		model.setDiscount(discount);
		return model;
	}
	
}
